//
//  SXStatusCell.swift
//  sina
//
//  Created by shengxinwei on 16/5/23.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit
import SnapKit

class SXStatusCell: UITableViewCell {

    
    //声明约束对象属性
    var toolbarTopCons: Constraint?
    
    var statusViewModel: SXStatusViewModel? {
        didSet {
            statusView.statusViewModel = statusViewModel
            toolbarView.statusViewModel = statusViewModel
            
            toolbarTopCons?.uninstall()
            if  statusViewModel?.status?.retweeted_status == nil {
                retweetedView.statusViewModel = nil
                retweetedView.hidden = true
                toolbarView.snp_updateConstraints(closure: { (make) in
                    self.toolbarTopCons =  make.top.equalTo(statusView.snp_bottom).constraint
                })
            }else {
                 retweetedView.statusViewModel = statusViewModel
                retweetedView.hidden = false
                toolbarView.snp_updateConstraints(closure: { (make) in
                    self.toolbarTopCons =  make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            }
           
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
//        self.backgroundColor = randomColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        contentView.addSubview(statusView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(toolbarView)
        statusView.snp_makeConstraints { (make) in
            make.left.top.right.equalTo(self.contentView)
        }
        retweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(statusView.snp_bottom)
            make.left.right.equalTo(self.contentView)
        }
        
        toolbarView.snp_makeConstraints { (make) in
            //生成顶部约束 并且使用属性进行保存
            self.toolbarTopCons = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.left.right.equalTo(self.contentView)
            make.height.equalTo(35)
        }
        contentView.snp_makeConstraints { (make) in
            //将contentView的约束都加上
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(toolbarView.snp_bottom)
        }

        
    }
    
    //原创内容
    private lazy var statusView: SXStatusOriginalView = SXStatusOriginalView()
    //转发微博
    private lazy var retweetedView: SXStatusRetweetedView = SXStatusRetweetedView();
    //底部显示工具栏
    private lazy var toolbarView: SXStatusToolBar = SXStatusToolBar()
    

}
